<?php

class Post {

    private $id;
    private $author;
    private $date;
    private $dateGmt;
    private $content;
    private $title;
    private $excerpt;
    private $status;
    private $commentStatus;
    private $pingStatus;
    private $password;
    private $name;
    private $toPing;
    private $pinged;
    private $modified;
    private $modifiedGmt;
    private $contentFiltered;
    private $parent;
    private $guid;
    private $menuOrder;
    private $type;
    private $mimeType;
    private $commentCount;
    private $filter;

    public function __construct($properties = null) {
        if($properties != null){
            $this->initFromArray($this->convertNameProperties($properties));
        }
    }

    public function convertNameProperties($properties){
        if(is_array($properties) && count($properties)!= 0){
            $result = array();
            foreach($properties as $property => $value){

                if($property == 'ID'){
                    $new_property = 'id';
                }else{
                    $property  = str_replace('post_','', $property);
                    $property_arr = explode('_', $property);
                    $new_property = '';
                    foreach($property_arr as $key => $val){
                        if($key == 0){
                            $new_property .= $val;
                        }else{
                            $new_property .= ucfirst($val);
                        }
                    }
                }
                $result[$new_property] = $value;
            }

            return $result;
        }else{
            return null;
        }
    }

    public function initFromArray($properties){
        if(isset($properties['id'])){
            $this->id = $properties['id'];
        }
        if(isset($properties['author'])){
            $this->author = $properties['author'];
        }
        if(isset($properties['date'])){
            $this->date = $properties['date'];
        }
        if(isset($properties['dateGmt'])){
            $this->dateGmt = $properties['dateGmt'];
        }
        if(isset($properties['content'])){
            $this->content = $properties['content'];
        }
        if(isset($properties['title'])){
            $this->title = $properties['title'];
        }
        if(isset($properties['excerpt'])){
            $this->excerpt = $properties['excerpt'];
        }
        if(isset($properties['status'])){
            $this->status = $properties['status'];
        }
        if(isset($properties['commentStatus'])){
            $this->commentStatus = $properties['commentStatus'];
        }
        if(isset($properties['pingStatus'])){
            $this->pingStatus = $properties['pingStatus'];
        }
        if(isset($properties['password'])){
            $this->password = $properties['password'];
        }
        if(isset($properties['name'])){
            $this->name = $properties['name'];
        }
        if(isset($properties['toPing'])){
            $this->toPing = $properties['toPing'];
        }
        if(isset($properties['pinged'])){
            $this->pinged = $properties['pinged'];
        }
        if(isset($properties['modified'])){
            $this->modified = $properties['modified'];
        }
        if(isset($properties['modifiedGmt'])){
            $this->modifiedGmt = $properties['modifiedGmt'];
        }
        if(isset($properties['contentFiltered'])){
            $this->contentFiltered = $properties['contentFiltered'];
        }
        if(isset($properties['parent'])){
            $this->parent = $properties['parent'];
        }
        if(isset($properties['guid'])){
            $this->guid = $properties['guid'];
        }
        if(isset($properties['menuOrder'])){
            $this->menuOrder = $properties['menuOrder'];
        }
        if(isset($properties['type'])){
            $this->type = $properties['type'];
        }
        if(isset($properties['mimeType'])){
            $this->mimeType = $properties['mimeType'];
        }
        if(isset($properties['commentCount'])){
            $this->commentCount = $properties['commentCount'];
        }
        if(isset($properties['filter'])){
            $this->filter = $properties['filter'];
        }
    }

    public function initFromXml($xml, $from = "file"){
        if($from == "url" || $from == "file"){
            $xdom = simplexml_load_file($xml);
        }
        if($from == "string"){
            $xdom = simplexml_load_string($xml);
        }
        $this->id = (string)$xdom->id;
        $this->author = (string)$xdom->author;
        $this->date = (string)$xdom->date;
        $this->dateGmt = (string)$xdom->dateGmt;
        $this->content = (string)$xdom->content;
        $this->title = (string)$xdom->title;
        $this->excerpt = (string)$xdom->excerpt;
        $this->status = (string)$xdom->status;
        $this->commentStatus = (string)$xdom->commentStatus;
        $this->pingStatus = (string)$xdom->pingStatus;
        $this->password = (string)$xdom->password;
        $this->name = (string)$xdom->name;
        $this->toPing = (string)$xdom->toPing;
        $this->pinged = (string)$xdom->pinged;
        $this->modified = (string)$xdom->modified;
        $this->modifiedGmt = (string)$xdom->modifiedGmt;
        $this->contentFiltered = (string)$xdom->contentFiltered;
        $this->parent = (string)$xdom->parent;
        $this->guid = (string)$xdom->guid;
        $this->menuOrder = (string)$xdom->menuOrder;
        $this->type = (string)$xdom->type;
        $this->mimeType = (string)$xdom->mimeType;
        $this->commentCount = (string)$xdom->commentCount;
        $this->filter = (string)$xdom->filter;
    }

    public function initFromJson($json,$from = "file"){
        if($from == "string"){
            $array = json_decode($json,true);
        }

        if($from == "file" || $from == "url"){
            $js_array = file_get_contents($json);
            $array = json_decode($js_array,true);
        }

        foreach($array as $value){
            $data_array = $value[0];
        }
        $this->id = $data_array['id'];
        $this->author = $data_array['author'];
        $this->date = $data_array['date'];
        $this->dateGmt = $data_array['dateGmt'];
        $this->content = $data_array['content'];
        $this->title = $data_array['title'];
        $this->excerpt = $data_array['excerpt'];
        $this->status = $data_array['status'];
        $this->commentStatus = $data_array['commentStatus'];
        $this->pingStatus = $data_array['pingStatus'];
        $this->password = $data_array['password'];
        $this->name = $data_array['name'];
        $this->toPing = $data_array['toPing'];
        $this->pinged = $data_array['pinged'];
        $this->modified = $data_array['modified'];
        $this->modifiedGmt = $data_array['modifiedGmt'];
        $this->contentFiltered = $data_array['contentFiltered'];
        $this->parent = $data_array['parent'];
        $this->guid = $data_array['guid'];
        $this->menuOrder = $data_array['menuOrder'];
        $this->type = $data_array['type'];
        $this->mimeType = $data_array['mimeType'];
        $this->commentCount = $data_array['commentCount'];
        $this->filter = $data_array['filter'];
    }

    public function setId($value) {
        $this->id = $value;
    }

    public function getId() {
        return $this->id;
    }

    public function setAuthor($value) {
        $this->author = $value;
    }

    public function getAuthor() {
        return $this->author;
    }

    public function setDate($value) {
        $this->date = $value;
    }

    public function getDate() {
        return $this->date;
    }

    public function setDateGmt($value) {
        $this->dateGmt = $value;
    }

    public function getDateGmt() {
        return $this->dateGmt;
    }

    public function setContent($value) {
        $this->content = $value;
    }

    public function getContent() {
        return $this->content;
    }

    public function setTitle($value) {
        $this->title = $value;
    }

    public function getTitle() {
        return $this->title;
    }

    public function setExcerpt($value) {
        $this->excerpt = $value;
    }

    public function getExcerpt() {
        return $this->excerpt;
    }

    public function setStatus($value) {
        $this->status = $value;
    }

    public function getStatus() {
        return $this->status;
    }

    public function setCommentStatus($value) {
        $this->commentStatus = $value;
    }

    public function getCommentStatus() {
        return $this->commentStatus;
    }

    public function setPingStatus($value) {
        $this->pingStatus = $value;
    }

    public function getPingStatus() {
        return $this->pingStatus;
    }

    public function setPassword($value) {
        $this->password = $value;
    }

    public function getPassword() {
        return $this->password;
    }

    public function setName($value) {
        $this->name = $value;
    }

    public function getName() {
        return $this->name;
    }

    public function setToPing($value) {
        $this->toPing = $value;
    }

    public function getToPing() {
        return $this->toPing;
    }

    public function setPinged($value) {
        $this->pinged = $value;
    }

    public function getPinged() {
        return $this->pinged;
    }

    public function setModified($value) {
        $this->modified = $value;
    }

    public function getModified() {
        return $this->modified;
    }

    public function setModifiedGmt($value) {
        $this->modifiedGmt = $value;
    }

    public function getModifiedGmt() {
        return $this->modifiedGmt;
    }

    public function setContentFiltered($value) {
        $this->contentFiltered = $value;
    }

    public function getContentFiltered() {
        return $this->contentFiltered;
    }

    public function setParent($value) {
        $this->parent = $value;
    }

    public function getParent() {
        return $this->parent;
    }

    public function setGuid($value) {
        $this->guid = $value;
    }

    public function getGuid() {
        return $this->guid;
    }

    public function setMenuOrder($value) {
        $this->menuOrder = $value;
    }

    public function getMenuOrder() {
        return $this->menuOrder;
    }

    public function setType($value) {
        $this->type = $value;
    }

    public function getType() {
        return $this->type;
    }

    public function setMimeType($value) {
        $this->mimeType = $value;
    }

    public function getMimeType() {
        return $this->mimeType;
    }

    public function setCommentCount($value) {
        $this->commentCount = $value;
    }

    public function getCommentCount() {
        return $this->commentCount;
    }

    public function setFilter($value) {
        $this->filter = $value;
    }

    public function getFilter() {
        return $this->filter;
    }

    public function toArray(){
        $post = array();
        $post['id'] = $this->id;
        $post['author'] = $this->author;
        $post['date'] = $this->date;
        $post['dateGmt'] = $this->dateGmt;
        $post['content'] = $this->content;
        $post['title'] = $this->title;
        $post['excerpt'] = $this->excerpt;
        $post['status'] = $this->status;
        $post['commentStatus'] = $this->commentStatus;
        $post['pingStatus'] = $this->pingStatus;
        $post['password'] = $this->password;
        $post['name'] = $this->name;
        $post['toPing'] = $this->toPing;
        $post['pinged'] = $this->pinged;
        $post['modified'] = $this->modified;
        $post['modifiedGmt'] = $this->modifiedGmt;
        $post['contentFiltered'] = $this->contentFiltered;
        $post['parent'] = $this->parent;
        $post['guid'] = $this->guid;
        $post['menuOrder'] = $this->menuOrder;
        $post['type'] = $this->type;
        $post['mimeType'] = $this->mimeType;
        $post['commentCount'] = $this->commentCount;
        $post['filter'] = $this->filter;
        return $post;
    }

    public function toXml(){
        $xml_string = '<post>';
        $xml_string .= '<id>'.$this->id.'</id>';
        $xml_string .= '<author>'.$this->author.'</author>';
        $xml_string .= '<date>'.$this->date.'</date>';
        $xml_string .= '<dateGmt>'.$this->dateGmt.'</dateGmt>';
        $xml_string .= '<content>'.$this->content.'</content>';
        $xml_string .= '<title>'.$this->title.'</title>';
        $xml_string .= '<excerpt>'.$this->excerpt.'</excerpt>';
        $xml_string .= '<status>'.$this->status.'</status>';
        $xml_string .= '<commentStatus>'.$this->commentStatus.'</commentStatus>';
        $xml_string .= '<pingStatus>'.$this->pingStatus.'</pingStatus>';
        $xml_string .= '<password>'.$this->password.'</password>';
        $xml_string .= '<name>'.$this->name.'</name>';
        $xml_string .= '<toPing>'.$this->toPing.'</toPing>';
        $xml_string .= '<pinged>'.$this->pinged.'</pinged>';
        $xml_string .= '<modified>'.$this->modified.'</modified>';
        $xml_string .= '<modifiedGmt>'.$this->modifiedGmt.'</modifiedGmt>';
        $xml_string .= '<contentFiltered>'.$this->contentFiltered.'</contentFiltered>';
        $xml_string .= '<parent>'.$this->parent.'</parent>';
        $xml_string .= '<guid>'.$this->guid.'</guid>';
        $xml_string .= '<menuOrder>'.$this->menuOrder.'</menuOrder>';
        $xml_string .= '<type>'.$this->type.'</type>';
        $xml_string .= '<mimeType>'.$this->mimeType.'</mimeType>';
        $xml_string .= '<commentCount>'.$this->commentCount.'</commentCount>';
        $xml_string .= '<filter>'.$this->filter.'</filter>';
        $xml_string .= '</post>';
        return $xml_string;
    }

    public function toJson(){
        $json_string = '{"post":[{';
        $json_string .= '"id":"'.$this->id.'",';
        $json_string .= '"author":"'.$this->author.'",';
        $json_string .= '"date":"'.$this->date.'",';
        $json_string .= '"dateGmt":"'.$this->dateGmt.'",';
        $json_string .= '"content":"'.$this->content.'",';
        $json_string .= '"title":"'.$this->title.'",';
        $json_string .= '"excerpt":"'.$this->excerpt.'",';
        $json_string .= '"status":"'.$this->status.'",';
        $json_string .= '"commentStatus":"'.$this->commentStatus.'",';
        $json_string .= '"pingStatus":"'.$this->pingStatus.'",';
        $json_string .= '"password":"'.$this->password.'",';
        $json_string .= '"name":"'.$this->name.'",';
        $json_string .= '"toPing":"'.$this->toPing.'",';
        $json_string .= '"pinged":"'.$this->pinged.'",';
        $json_string .= '"modified":"'.$this->modified.'",';
        $json_string .= '"modifiedGmt":"'.$this->modifiedGmt.'",';
        $json_string .= '"contentFiltered":"'.$this->contentFiltered.'",';
        $json_string .= '"parent":"'.$this->parent.'",';
        $json_string .= '"guid":"'.$this->guid.'",';
        $json_string .= '"menuOrder":"'.$this->menuOrder.'",';
        $json_string .= '"type":"'.$this->type.'",';
        $json_string .= '"mimeType":"'.$this->mimeType.'",';
        $json_string .= '"commentCount":"'.$this->commentCount.'",';
        $json_string .= '"filter":"'.$this->filter.'"';
        $json_string .= '}]}';
        return $json_string;
    }

    public function toHtmlRow(){
        $row = "<tr>";
        $row .= "<td>".$this->id."</td>";
        $row .= "<td>".$this->author."</td>";
        $row .= "<td>".$this->date."</td>";
        $row .= "<td>".$this->dateGmt."</td>";
        $row .= "<td>".$this->content."</td>";
        $row .= "<td>".$this->title."</td>";
        $row .= "<td>".$this->excerpt."</td>";
        $row .= "<td>".$this->status."</td>";
        $row .= "<td>".$this->commentStatus."</td>";
        $row .= "<td>".$this->pingStatus."</td>";
        $row .= "<td>".$this->password."</td>";
        $row .= "<td>".$this->name."</td>";
        $row .= "<td>".$this->toPing."</td>";
        $row .= "<td>".$this->pinged."</td>";
        $row .= "<td>".$this->modified."</td>";
        $row .= "<td>".$this->modifiedGmt."</td>";
        $row .= "<td>".$this->contentFiltered."</td>";
        $row .= "<td>".$this->parent."</td>";
        $row .= "<td>".$this->guid."</td>";
        $row .= "<td>".$this->menuOrder."</td>";
        $row .= "<td>".$this->type."</td>";
        $row .= "<td>".$this->mimeType."</td>";
        $row .= "<td>".$this->commentCount."</td>";
        $row .= "<td>".$this->filter."</td>";
        $row .= "</tr>";
        return $row;
    }
}
